//
//  LearnMoreView.swift
//  CrseAsst
//
//  Created by 申铭 on 2024/3/4.
//

import SwiftUI
import AlertToast
//import SwiftUIIntrospect

struct LearnMoreView: View {
    @State private var showResultsPage = false
    
    @State private var showToast = false
    @State private var alertType: AlertToast.AlertType = .regular
    @State private var alertInfo: String = ""
    
    @State private var results = [ImportResult]()
    
    var body: some View {
        List {
            Section {
                LearnMoreRow(title: "标题标题", subtitle: "子标题子标题", action: {
                    Task {
                        if let arr = await ImportTool.importIdiom() {
                            showToast = true
                            alertType = .complete(.green)
                            results = arr
                        } else {
                            showToast = true
                            alertType = .error(.red)
                            alertInfo = "导入失败"
                        }
                    }
                })
            }
        }
        .toast(isPresenting: $showToast, duration: UIConst.hudDuration, alert: {
            AlertToast(type: alertType, title: alertInfo)
        }, completion:  {
            showResultsPage = true
        })
        
        NavigationLink("", destination: ImportResultView(results: results), isActive: $showResultsPage)
            .opacity(1)
    }
    
}

private struct LearnMoreRow: View {
    let title: String
    let subtitle: String
    let action: () -> Void
    
    var body: some View {
        Button(action: {
            action()
        }, label: {
            VStack(alignment: .leading, content: {
                Text(title)
                    .font(.system(size: 17))
                    .foregroundColor(.primary)
                    .padding(.bottom, 1)
                Text(subtitle)
                    .font(.system(size: 15))
                    .foregroundColor(.secondary)
            })
        })
    }
}

#Preview {
    LearnMoreView()
//    LearnMoreRow(title: "标题标题", subtitle: "子标题子标题")
}
